﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class aspnet_admin_bock_keyWord_CX_keyWord_AddNew : System.Web.UI.Page
{
    public Tong.TongUse TextData = new Tong.TongUse();
    protected void Page_Load(object sender, EventArgs e)
    {
        Palen.InnerHtml = CreateCheckBox("6");
        if (IsPostBack)
        {
            PageSave();
        }
    }



    protected void PageSave()
    {

        string Check = Request.Form["keyWord_Name"];//得到所有的分类
        if (Check== null)
        {
            tt.Text = "请选择分类";
        }
        else
        {

            if (Cx_keyWord_Name.Text != "" && Cx_keyWord_Name.Text.Length < 250 && TextData.SqlataSet("select Cx_keyWord_Name from  [CX_keyWord] where Cx_keyWord_Name='" + Cx_keyWord_Name.Text.Trim() + "'").Tables[0].Rows.Count == 0)
            {
                try
                {
                    int.Parse(Cx_keyWord_Sort.Text.Trim());
                    if (int.Parse(Cx_keyWord_Sort.Text.Trim()) <= 99999)
                    {
                        TextData.SqlDataSave("insert into [CX_keyWord] (Cx_keyWord_Name,Cx_keyWord_Sort,CX_keyWord_Class_id) values('" + Cx_keyWord_Name.Text.Trim() + "'," + Cx_keyWord_Sort.Text.Trim() + ",'" + Check + "')");
                        tt.Text = "添加关键字" + Cx_keyWord_Name.Text + "成功";
                        Cx_keyWord_Name.Text = "";

                    }
                    else
                    {
                        tt.Text = "排序超出范围";
                    }
                }
                catch
                {
                    tt.Text = "排序超出范围";
                }
 
            }
            else
            {
                tt.Text = "输入的关键字为空或超过250个或以存在相同关键字";

            }
        }
    }

    /// <summary>
    /// 传入 以选的ID
    /// 创建 text box;
    /// </summary>
    /// <![CDATA[ {1,2,547,8,79,79,98,4 }    CreateCheckBox("5");
    /// ]]>
    internal string  CreateCheckBox(string key_id)
    {
        string[] Cx_id;
        string str = "";
        Cx_id = key_id.Split(',');//以 ' 分开 得到数组

        DataTable KeyClass = TextData.SqlataSet("select CX_keyWord_Class_id,CX_keyWord_Class_Name from [CX_keyWord_Class]").Tables[0];
        for (int i = 0; i < KeyClass.Rows.Count; i++)
        {



            if (ISCheckBoxCreate(Cx_id, KeyClass.Rows[i]["CX_keyWord_Class_id"].ToString().Trim()))//如果选定
            {
                str += "<input id=\"keyWord_Name\" name=\"keyWord_Name\" type=\"checkbox\" value=\"" + KeyClass.Rows[i]["CX_keyWord_Class_id"].ToString().Trim() + "\" checked=\"checked\" /> " + KeyClass.Rows[i]["CX_keyWord_Class_Name"].ToString().Trim() + "   ";

            }
            else
            {
                str += "<input id=\"keyWord_Name\" name=\"keyWord_Name\" type=\"checkbox\" value=\"" + KeyClass.Rows[i]["CX_keyWord_Class_id"].ToString().Trim() + "\"  /> " + KeyClass.Rows[i]["CX_keyWord_Class_Name"].ToString().Trim() + "   ";
            }

        }
        return str;

    }

    /// <summary>
    ///  看看数组里是不是存在 这个ID
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    protected bool ISCheckBoxCreate(string[] shuZhu,string id)
    {
        for (int i = 0; i < shuZhu.Length; i++)
        {
            if (id == shuZhu[i])
            {
                return true;
            }
        }
        return false;
    }

}
